<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validateTrigger="bind">
      <uni-forms-item name="title" label="">
        <uni-easyinput placeholder="商品标题" v-model="formData.title"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="pay_type" label="">
        <uni-easyinput placeholder="支付渠道，wxpay | alipay" v-model="formData.pay_type"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="total_fee" label="">
        <uni-easyinput placeholder="订单总金额，单位：分。" type="number" v-model="formData.total_fee"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="platform" label="">
        <undefined v-model="formData.platform"></undefined>
      </uni-forms-item>
      <uni-forms-item name="status" label="">
        <uni-easyinput placeholder="订单状态，1：待付款，2：已付款，3：退款审核中，4：退款中，5：已退款，-1已取消付款/退款，-2：退款拒绝，-3：退款失败" type="number" v-model="formData.status"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="update_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.update_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="paid_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.paid_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="transaction_id" label="">
        <undefined v-model="formData.transaction_id"></undefined>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="">
        <undefined v-model="formData.user_id"></undefined>
      </uni-forms-item>
      <uni-forms-item name="address_user_id" label="">
        <undefined v-model="formData.address_user_id"></undefined>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
            <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/uni-id-base-order.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'uni-id-base-order';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  export default {
    data() {
      let formData = {
        "title": "",
        "pay_type": "",
        "total_fee": null,
        "platform": null,
        "status": null,
        "create_time": null,
        "update_time": null,
        "paid_time": null,
        "transaction_id": null,
        "user_id": null,
        "address_user_id": null
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 触发表单提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          this.submitForm(res)
        }).catch(() => {
          uni.hideLoading()
        })
      },

      submitForm(value) {
        // 使用 clientDB 提交数据
        db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("title,pay_type,total_fee,platform,status,create_time,update_time,paid_time,transaction_id,user_id,address_user_id").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
